<template>
  <el-dialog :visible.sync="dialog" :disabled="!form.busPageInfo.editable" :close-on-click-modal="false" title="自定义表单维护" width="600px" append-to-body >
    <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="表单名称" prop="busMainData.formName">
        <el-input :maxlength="100" :style="{width: '100%'}" v-model="form.busMainData.formName" placeholder="请输入表单名称" show-word-limit clearable />
      </el-form-item>
      <el-form-item label="表单类型" prop="busMainData.formType">
        <el-select v-model="form.busMainData.formType" :disabled="form.funProcess != '10'" :style="{width: '100%'}" placeholder="请选表单类型">
          <el-option
            v-for="(item) in dicts.form_type"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"/>
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.busMainData.formType === 'OA'" label="OA菜单名称" prop="busMainData.oaMenuName">
        <el-input :maxlength="100" :style="{width: '100%'}" v-model="form.busMainData.oaMenuName" placeholder="请输入OA菜单名称" show-word-limit clearable/>
      </el-form-item>
      <el-form-item v-if="form.busMainData.formType === 'EXT'" :disabled="form.funProcess != '10'" label="扩展模块" prop="busMainData.moduleBusClass">
        <el-select v-model="form.busMainData.moduleBusClass" :style="{width: '100%'}" placeholder="请选扩展模块">
          <el-option
            v-for="(item) in moduleMenuList"
            :key="item.moduleBusClass"
            :label="item.menuName"
            :value="item.moduleBusClass"/>
        </el-select>
      </el-form-item>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','FORM_SET_ALL','FORM_SET_CHANGE','FORM_SET_CREATE']" :loading="loading" type="primary" @click="doSave">暂存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrEdit, viewMainData, toAdd, toChange, toCopy } from '@/api/system/formSet'
import { getAllExtendMenu } from '@/api/system/menu'

export default {
  components: { },
  dictTypes: ['form_type'],
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isAdd: true,
      loading: false,
      formLoading: false,
      dialog: false,
      moduleMenuList: [],
      form: {
        funProcess: '10',
        formSubmit: false,
        busPageInfo: {
          editable: true
        },
        formSetContentVO: {
          formContent: '',
          formHtml: '',
          formJson: ''
        },
        busMainData: {
          id: null,
          formName: '',
          formType: '',
          moduleBusClass: '',
          oaMenuName: '',
          enableFlag: 'Y'
        },
        busCommonState: {
        }
      },
      rules: {
        'busMainData.formName': [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ],
        'busMainData.oaMenuName': [
          { required: true, message: '请输入OA菜单名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ],
        'busMainData.moduleBusClass': [
          { required: true, message: '请选扩展模块', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
      getAllExtendMenu().then(res => {
        this.moduleMenuList = res.data
      })
    },
    viewMainData(mainId) {
      const data = { paramBean: { id: mainId }}
      viewMainData(data).then(res => {
        this.form = res.data
        if (this.form.busPageInfo.editable) {
          this.getFeildDetailInfo(mainId)
        }
      })
    },
    getFeildDetailInfo(mainId) {
    },
    addData() {
      const data = { paramBean: { }}
      toAdd(data).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err.response)
      })
    },
    copyData(mainId) {
      const data = { paramBean: { id: mainId }}
      toCopy(data).then(res => {
        this.dialog = true
        this.form = res.data
        this.getFeildDetailInfo(mainId)
      }).catch(err => {
        this.dialog = false
        console.log(err.response)
      })
    },
    changeData(busId) {
      const data = { paramBean: { id: busId }}
      toChange(data).then(res => {
        this.dialog = true
        this.form = res.data
        this.getFeildDetailInfo(this.form.busMainData.oldMainId)
      }).catch(err => {
        this.dialog = false
        console.log(err.response)
      })
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>
